package org.apache.struts.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//
//import org.apache.log4j.Logger;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory; 







import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import controller.StackTracePatternServlet;
import vo.StuckEventVo;
import dao.hibernate.StuckEventVoDAO;

public class ActionServlet extends HttpServlet {
	private static Logger LOGGER = LoggerFactory.getLogger(ActionServlet.class);
	
	/**
	 * Constructor of the object.
	 */
	public ActionServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

//		LOGGER.debug("doGet"  );
		String ip = request.getParameter("ip");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
//		LOGGER.debug("endDate:{}" ,endDate);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(final HttpServletRequest request, final HttpServletResponse response)
			throws ServletException, IOException {
//		LOGGER.debug("doPost"  );
		String ip = request.getParameter("ip");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		final StuckEventVoDAO dao = new StuckEventVoDAO();
		Map<Integer,List<StuckEventVo>> stacktraceUidListMap =new HashMap<Integer, List<StuckEventVo>>();
		if(ip!=null && ip.length()>0){
			final List<StuckEventVo> list = dao.findByHostIp(ip.trim(),startDate, endDate);
			LOGGER.debug("data size: {}" ,list.size());
			for(StuckEventVo vo : list){
				Integer stacktraceUid =	vo.getStacktraceUid();
				List<StuckEventVo> uidlist = stacktraceUidListMap.get(stacktraceUid);
				if(uidlist == null ){
					uidlist =new ArrayList<StuckEventVo>();
				}
				uidlist.add(vo);
				stacktraceUidListMap.put(stacktraceUid, uidlist);
			}
		}
		request.getSession().setAttribute("stacktraceUidListMap", stacktraceUidListMap);
		RequestDispatcher dispatcher = request.getRequestDispatcher("MyJsp.jsp");
	    dispatcher.forward(request, response);
	}

}
